﻿// Show jitter and firing failures in PNG activation
// Generates multiple frames of structures at the same timepoint in the course of training

// Note that DLL reference order is very important
#r @"C:\Users\Mira\Source\Repos\Spinula\Debug\SpikingNeuronLib.dll"
#r @"C:\Users\Mira\Source\Repos\Spinula\SpikingAnalyticsBaseLib\bin\Debug\SpikingAnalyticsBaseLib.dll"
#r @"C:\Users\Mira\Source\Repos\Spinula\SpikingAnalyticsLib\bin\Debug\SpikingAnalyticsLib.dll"
#I @"C:\Users\Mira\Source\Repos\Spinula\packages\RProvider.1.0.12"
#load "RProvider.fsx"
#I @"C:\Users\Mira\Source\Repos\Spinula\packages\Deedle.1.0.0"
#load "Deedle.fsx"
#r @"C:\Users\Mira\Source\Repos\Spinula\SpikingVisualisationRLib\bin\Debug\SpikingVisualisationRLib.dll"    // references Deedle
#r @"C:\Users\Mira\Source\Repos\Spinula\SpikingAnalyticsFrameLib\bin\Debug\SpikingAnalyticsFrameLib.dll"    // references Deedle

open System
open System.IO
open SpikingAnalyticsLib
open SpikingAnalyticsLib.ResponseFingerPrint
open SpikingAnalyticsFrameLib
open SpikingVisualisationRLib

// Show PNG activation over multiple frames for the specified network state
// Ideally the selected network will only be partially trained e.g. 30 secs at 5 Hz
// otherwise the connections will be too dense to clearly see individual firing events
let ShowPNGActivation pathToNetworkStateFile pathToFrameProfileFile =

    let numberOfFrames = 4          // show this many frames
    let synapticThreshold = 9.9     // exclusive threshold: weights must be greater than this value

    let network = CrossbarNetwork.CreateFromFile(CrossbarNetworkSpecifier.N1000Network, pathToNetworkStateFile)

    // collect firing data in response to the selected pattern
    // and create a list of activation-related firing events for each frame
    let frameGroups =
        let profile = FrameProfile.Load(pathToFrameProfileFile)
        let classifier = NaiveBayesClassifier(network)
        classifier.GetIndividualFrameGroups(numberOfFrames, profile)

    // display each frame
    for index in 0..numberOfFrames-1 do

        // generate a PNG structural description (saturated weights only)
        let groupDescriptor =
            network.GetPNGDescriptor(frameGroups, index, synapticThreshold)

        // transform the data into the required form
        let plotDataFrame = PNGDescriptorFrame.GetFrame(groupDescriptor)

        // show the plot
        VisualisationUtilities.NewWindow()
        PNGVisualisation.PlotPNG(plotDataFrame, 0, 50)

        let savePath =
            let outputFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
            let fileBasename = sprintf "descriptor_%d" index
            PathDescriptor.Create(outputFolder, fileBasename)

        // save the network graph data
        groupDescriptor.Save(savePath)

        // save the plot
        VisualisationUtilities.SavePlot(savePath, SaveFormat.Jpg)


let inputFolder = @"C:\Users\Mira\Desktop\Coursework\2011-13\Thesis\ScriptData\Metaplasticity\TrainNetworkWithMeta"
let stateFilePath = Path.Combine(inputFolder, "Network0NoMetaState181.txt")
let profilePath = Path.Combine(inputFolder, "profile_Network0NoMeta_Ascending.txt")
ShowPNGActivation stateFilePath profilePath
